package uk.ac.aber.paws.server.util;

import java.text.NumberFormat;
import java.util.Locale;
import java.util.StringTokenizer;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 21st May 2012
 * 
 * TextManip.java
 * 
 * General text manipulation procedures
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class TextManip {

	/**
	 * Converts a float into the local english currency
	 * @param valIn The float to be converted
	 * @return The currency in the form of a string
	 */
	public static String formatCurrencyFromFloat(float valIn) {
		
		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.ENGLISH);
	    
		String tmpString = nf.format(valIn);
		
		tmpString = tmpString.substring(1,tmpString.length());
		
		return tmpString;
	}
	
	/**
	 * Converts a LDAP context path to a FQDN.
	 * @param ldap The OU context
	 * @return The fqdn
	 */
	public static String convertToLDAPtoFQDN(String ldap) {
		
		String fqdn = "";
		
		StringTokenizer st = new StringTokenizer(ldap,",");
		
		// get first element - computer name
		StringTokenizer st2 = new StringTokenizer((String)st.nextElement(),"=");
		
		if (st2.countTokens() == 2) {
			if (st2.nextElement().equals("CN")) {
				fqdn = (String)st2.nextElement();
			}
		}
		
		while (st.hasMoreElements()) {
			
			st2 = new StringTokenizer((String)st.nextElement(),"=");
			
			if (st2.countTokens() == 2) {
				
				String identifier = ((String)st2.nextElement()).toUpperCase();
				if (identifier.equals("DC")) {
					fqdn += "."+(String)st2.nextElement();
				}
			}
		}
		
		return fqdn;
	}
	
	/**
	 * Extracts the computer name from teh FQDN
	 * @param The source FQDN
	 * @return The computer name
	 */
	public static String extractComputerNameFromFQDN(String fqdn) {
		
		StringTokenizer st = new StringTokenizer(fqdn,".");
		
		// get first element - computer name
		if (st.countTokens() > 1) {
			return (String)st.nextElement();
		}
		
		return null;
	}
	
	/**
	 * Converts a UNIX style file path to DOS
	 * @param thePath The unix path
	 * @return The DOS path
	 */
	public static String convertUNIXtoDOSpath(String thePath) {
		
		char[] tmpChars = thePath.toCharArray();
		for (int i=0;i<tmpChars.length;i++) {
			if (tmpChars[i] == '/') {
				tmpChars[i] = '\\';
			}
		}
	
		return new String(tmpChars);
	}
}
